home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / plan / src / file_r.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  8KB  |  289 lines

  1. /*
  2.  * read the master list from the ~/.schedule file. This file is also
  3.  * linked into the daemon program (file_w.c is not).
  4.  *
  5.  *    readfile(list, path)        Read a file into the list.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #ifdef MIPS
  12. #include <sys/types.h>
  13. #include <sys/fcntl.h>
  14. #else
  15. #include <stdlib.h>
  16. #endif
  17. #include <time.h>
  18. #include "conf.h"
  19.  
  20. #ifdef MIPS
  21. extern char        *getenv();
  22. extern char        *malloc();
  23. #endif
  24. extern char        *mystrdup();
  25. extern time_t        tm_to_time();
  26. extern char        *resolve_tilde();    /* evaluates path with ~ */
  27. extern char        *progname;        /* argv[0] */
  28. extern struct config    config;            /* global configuration data */
  29. struct user        *user;            /* user list for week view */
  30. int            nusers;            /* # of users in user list */
  31. char            Print_Spooler[100];    /* print spooling command */
  32.  
  33.  
  34. /*
  35.  * read a list from a file. If <list> points to a non-null pointer, the
  36.  * list in the file is merged into the list. As with add_entry(), <list>
  37.  * is a pointer to a pointer to the list because the list might grow and
  38.  * must be re-allocated. If the file is empty, no new list is allocated,
  39.  * and <list> continues to point to a null pointer.
  40.  * If conf is TRUE, read the config parameters too, otherwise ignore them.
  41.  * This is used when other user's .dayplan files are read for the week view.
  42.  * Returns FALSE if the file could not be read.
  43.  */
  44.  
  45. BOOL readfile(list, path, conf)
  46.     struct list        **list;        /* list to add to */
  47.     char            *path;        /* file to read list from */
  48.     BOOL            conf;        /* if TRUE, config stuff too */
  49. {
  50.     register struct config    *c = &config;
  51.     FILE            *fp;        /* open file */
  52.     BOOL            writable;    /* can we lock the file? */
  53.     char            line[1024];    /* line buffer */
  54.     register char        *p;        /* line scan pointer */
  55.     register struct entry    *ep = 0;    /* list entry being read */
  56.     int            n;        /* tmp entry # of ep in list */
  57.     struct entry        entry;        /* temp entry before adding */
  58.     struct tm        tm;        /* m/d/y h:m:s to time conv */
  59.     int            tmp[9];        /* temps for sscanf */
  60.     char            sflag, pflag;    /* more temps for sscanf */
  61.     char            nflag;        /* more temps for sscanf */
  62.     char            **string;    /* string to add to */
  63.     int            len;        /* new length of string */
  64.     int            user_i = 0;    /* next user to read */
  65.     char            buf1[256], buf2[1024];
  66.  
  67.     path = resolve_tilde(path);
  68.     writable = !access(path, W_OK);
  69.     if (!(fp = fopen(path, writable ? "r+" : "r")))
  70.         return(FALSE);
  71.     if (conf)        /* defaults for 't' line */
  72.         guess_tzone();
  73.     if (writable)
  74.         lockfile(fp, TRUE);
  75.     for (;;) {
  76.         if (!fgets(line, 1024, fp))
  77.             break;
  78.         if (!conf && strchr("otelapmUu", *line))
  79.             continue;
  80.         switch(*line) {
  81.           case '\n':
  82.           case '#':
  83.             continue;
  84.  
  85.           case 'o':
  86.             c->sunday_first = line[2]  != '-';
  87.             c->ampm         = line[3]  != '-';
  88.             c->mmddyy       = line[4]  != '-';
  89.             c->autodel      = line[5]  != '-';
  90.             c->julian       = line[6]  != '-';
  91.             c->weeknum      = line[7]  != '-';
  92.             c->nopast       = line[8]  != '-';
  93.             c->bigwarning   = line[9]  != '-';
  94.             c->weekwarn     = line[10] != '-';
  95.             c->weekuser     = line[11] != '-';
  96.             c->smallmonth   = line[12] != '-';
  97.             sscanf(line+16, "%d %d %d %d %d",
  98.                         (int *)&c->early_time,
  99.                         (int *)&c->late_time,
  100.                         (int *)&c->wintimeout,
  101.                         (int *)&c->week_minhour,
  102.                         (int *)&c->week_maxhour);
  103.             if (c->week_minhour >= c->week_maxhour) {
  104.                 c->week_minhour = 8;
  105.                 c->week_maxhour = 20;
  106.             }
  107.             break;
  108.  
  109.           case 't':
  110.             sscanf(line+2, "%d %d %d %d %d",
  111.                         (int *)&c->adjust_time,
  112.                         (int *)&c->raw_tzone,
  113.                         (int *)&c->dst_flag,
  114.                         (int *)&c->dst_begin,
  115.                         (int *)&c->dst_end);
  116.             break;
  117.  
  118.           case 'e':
  119.             c->ewarn_window = line[2] != '-';
  120.             c->ewarn_mail   = line[3] != '-';
  121.             c->ewarn_exec   = line[4] != '-';
  122.             line[strlen(line)-1] = 0;
  123.             if (c->ewarn_prog)
  124.                 free(c->ewarn_prog);
  125.             c->ewarn_prog = 0;
  126.             if (line[5] && line[6])
  127.                 c->ewarn_prog = mystrdup(line+6);
  128.             break;
  129.  
  130.           case 'l':
  131.             c->lwarn_window = line[2] != '-';
  132.             c->lwarn_mail   = line[3] != '-';
  133.             c->lwarn_exec   = line[4] != '-';
  134.             line[strlen(line)-1] = 0;
  135.             if (c->lwarn_prog)
  136.                 free(c->lwarn_prog);
  137.             c->lwarn_prog = 0;
  138.             if (line[5] && line[6])
  139.                 c->lwarn_prog = mystrdup(line+6);
  140.             break;
  141.  
  142.           case 'a':
  143.             c->alarm_window = line[2] != '-';
  144.             c->alarm_mail   = line[3] != '-';
  145.             c->alarm_exec   = line[4] != '-';
  146.             line[strlen(line)-1] = 0;
  147.             if (c->alarm_prog)
  148.                 free(c->alarm_prog);
  149.             c->alarm_prog = 0;
  150.             if (line[5] && line[6])
  151.                 c->alarm_prog = mystrdup(line+6);
  152.             break;
  153.  
  154.           case 'p':
  155.             strncpy(Print_Spooler, (const char *)line + 2,
  156.                         sizeof(Print_Spooler));
  157.             Print_Spooler[sizeof(Print_Spooler)-1] = 0;
  158.             Print_Spooler[strlen(Print_Spooler)-1] = 0;
  159.             break;
  160.  
  161.           case 'm':
  162.             line[strlen(line)-1] = 0;
  163.             if (line[1] && line[2])
  164.                 c->mailer = mystrdup(line+2);
  165.             break;
  166.  
  167.           case 'U':
  168.             if (user) {
  169.                 for (n=0; n < nusers; n++)
  170.                     if (user[n].list)
  171.                         free((char *)user[n].list);
  172.                 free((char *)user);
  173.                 user = 0;
  174.             }
  175.             sscanf(line+2, "%d", &nusers);
  176.             if (nusers) {
  177.                 if (!(user = (struct user *)malloc(
  178.                         nusers * sizeof(struct user))))
  179.                     fatal("no memory for %d users",nusers);
  180.                 for (n=0; n < nusers; n++) {
  181.                     user[n].name      = 0;
  182.                     user[n].home      = 0;
  183.                     user[n].suspended = 0;
  184.                     user[n].color     = 0;
  185.                     user[n].time      = 0;
  186.                     user[n].list      = 0;
  187.                 }
  188.             }
  189.             break;
  190.  
  191.           case 'u':
  192.             if (user_i >= nusers)
  193.                 fatal("too many u stmts in %s", path);
  194.             sscanf(line+2, "%s %s %d %d\n", buf1, buf2,
  195.                     &user[user_i].suspended,
  196.                     &user[user_i].color);
  197.             user[user_i].name = mystrdup(buf1);
  198.             user[user_i].home = mystrdup(buf2);
  199.             user_i++;
  200.             break;
  201.  
  202.           case '0':
  203.           case '1':
  204.           case '2':
  205.           case '3':
  206.           case '4':
  207.           case '5':
  208.           case '6':
  209.           case '7':
  210.           case '8':
  211.           case '9':
  212.             clone_entry(&entry, (struct entry *)0);
  213.             sscanf(line,
  214.              "%d/%d/%d %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:%d %c%c%c",
  215.                 &tm.tm_mon, &tm.tm_mday, &tm.tm_year,
  216.                 &tm.tm_hour, &tm.tm_min, &tm.tm_sec,
  217.                 tmp+0, tmp+1, tmp+2,
  218.                 tmp+3, tmp+4, tmp+5,
  219.                 tmp+6, tmp+7, tmp+8,
  220.                 &sflag, &pflag, &nflag);
  221.             tm.tm_mon--;
  222.             if (tm.tm_year < 70)
  223.                 tm.tm_year += 100;
  224.             if (tm.tm_year > 1900)
  225.                 tm.tm_year -= 1900;
  226.             if (entry.notime = tm.tm_hour > 23)
  227.                 tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  228.             entry.time         = tm_to_time(&tm);
  229.             entry.length       = tmp[0]*3600 + tmp[1]*60 + tmp[2];
  230.             entry.early_warn   = tmp[3]*3600 + tmp[4]*60 + tmp[5];
  231.             entry.late_warn    = tmp[6]*3600 + tmp[7]*60 + tmp[8];
  232.             entry.suspended    = sflag == 'S';
  233.             entry.private      = pflag == 'P';
  234.             entry.noalarm      = nflag == 'N';
  235.             n = add_entry(list, &entry);    /* sets <list> */
  236.             ep = &(*list)->entry[n];
  237.             break;
  238.  
  239.           case 'R':
  240.             sscanf(line+2, "%d %d %d %d %d",
  241.                 (int *)&ep->rep_every,
  242.                 (int *)&ep->rep_last,
  243.                 (int *)&ep->rep_weekdays,
  244.                 (int *)&ep->rep_days,
  245.                 (int *)&ep->rep_yearly);
  246.             break;
  247.  
  248.           case 'N':
  249.             string = &ep->note;
  250.             goto add;
  251.           case 'M':
  252.             string = &ep->message;
  253.             goto add;
  254.           case 'S':
  255.             string = &ep->script;
  256.             goto add;
  257.           case 'G':
  258.             string = &ep->meeting;
  259. add:            len  = *string ? strlen(*string) : 0;
  260.             len += strlen(line+2) + 2;
  261.             if (!(p = malloc(len))) {
  262.                 fprintf(stderr, "%s: no memory", progname);
  263.                 exit(1);
  264.             }
  265.             if (*string) {
  266.                 strcpy(p, *string);
  267.                 strcat(p, line+2);
  268.                 free(*string);
  269.             } else
  270.                 strcpy(p, line+2);
  271.             *string = p;
  272.             break;
  273.         
  274.           default:
  275.             fprintf(stderr, "%s: %s: illegal line:\n%s",
  276.                             progname, path, line);
  277.         }
  278.     }
  279.     if (writable)
  280.         lockfile(fp, FALSE);
  281.     fclose(fp);
  282.     if (conf)
  283.         set_tzone();
  284.     if (*list)
  285.         (*list)->modified = FALSE;
  286.     rebuild_repeat_chain(*list);
  287.     return(TRUE);
  288. }
  289.